package cn.sc.allinpay.allfavour.business.common.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.support.v7.widget.AppCompatTextView;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.util.List;

import cn.sc.allinpay.allfavour.R;
import cn.sc.allinpay.allfavour.business.common.data.CashierData;
import cn.sc.allinpay.allfavour.business.common.data.CheckCouponData;

/**
 * description：验券卡券结果弹出框
 * author：pany
 * on 2018/7/28 17:36
 */
public class CheckResultDialog extends Dialog implements View.OnClickListener {

    private List<CheckCouponData> datas;
    private CashierData operator;
    private AppCompatTextView tvOperator;
    private LinearLayout llContent;
    private Handler handler = new Handler();
    private OnDialogListener onDialogListener;

    public CheckResultDialog(@NonNull Context context, List<CheckCouponData> datas, CashierData operator) {
        super(context);
        this.datas = datas;
        this.operator = operator;
    }

    public CheckResultDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setCancelable(false);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.dialog_check_result);
        tvOperator = findViewById(R.id.tvOperator);
        tvOperator.setText(operator != null ? operator.getCashierName() : null);

        llContent = findViewById(R.id.llContent);
        for (int i = 0; i < datas.size(); i++) {
            CheckCouponData data = datas.get(i);
            RelativeLayout relat = new RelativeLayout(getContext());

            TextView tvName = new TextView(getContext());
            tvName.setTextSize(16);
            tvName.setText(data.getCouponName());
            relat.addView(tvName);

            RelativeLayout.LayoutParams lpRight = new RelativeLayout.LayoutParams(
                    RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
            lpRight.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
            TextView tvUse = new TextView(getContext());
            tvUse.setTextSize(16);
            if (data.getType() == 0) {
                tvUse.setText("-" + data.getUseAmt() + data.getUnit());
            } else {
                tvUse.setText("-" + data.getUseNumber() + data.getUnit());
            }
            relat.addView(tvUse, lpRight);
            llContent.addView(relat, new LinearLayout.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        }

        findViewById(R.id.btnConfirm).setOnClickListener(this);
        findViewById(R.id.btnCancel).setOnClickListener(this);
        findViewById(R.id.btnCancel).setVisibility(View.GONE);
        findViewById(R.id.llBtn).setVisibility(View.GONE);
    }

    @Override
    public void show() {
        super.show();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                dismiss();
                if (onDialogListener != null) {
                    onDialogListener.onConfirm();
                }
            }
        }, 1000);
    }

    public void setOnDialogListener(OnDialogListener onDialogListener) {
        this.onDialogListener = onDialogListener;
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btnConfirm:
                dismiss();
                if (onDialogListener != null) {
                    onDialogListener.onConfirm();
                }
                break;
            case R.id.btnCancel:
                dismiss();
                if (onDialogListener != null) {
                    onDialogListener.onCancel();
                }
                break;
        }
    }

    public static abstract class OnDialogListener {

        protected abstract void onConfirm();

        protected void onCancel() {
        }
    }
}
